<?php

/**
* 提款管理
*
* @package		Hooloo framework
* @author 		Bill
* @copyright 	Hooloo Co.,Ltd
* @version		1.0
* @release		2017.05.25
*/
defined('BASEPATH') OR exit('No direct script access allowed');

class Atm  extends Controller{
	public function __construct(){
		parent::__construct();
	}
	
	/**
     * 列表
     */
	public function index($aid = 0, $ispay = 9, $username = 0, $start_time = 0  ,$end_time = 0 , $p = 1) {
		//接收参数
		$aid = intval($aid);
		$ispay = intval($ispay);
		$p = intval($p);
		$username = urldecode(sql_format($username));
		
		//通用搜索
		$where = " where a.uid=b.id and a.aid=c.id";
		
		//存在代理
		$aid = (int)$aid <= 0 ? 0 : (int)$aid;
		if($aid){
			$where .= " AND a.aid = '$aid'";
		}
		
		//支付状态
		$ispay = (int)$ispay < 0 ? 9 : (int)$ispay;
		if($ispay != 9){
			$where .= " AND a.ispay = '$ispay'";
		}
		
		//用户搜索
		if($username){
			$where .= " AND b.username like '%$username%'";
		} else {
			$username = 0;
		}
		
		//搜索时间
		if (! $start_time) $start_time = date("Y-m-d"); 
		if (! $end_time) $end_time = date("Y-m-d");
		if ($start_time > $end_time) $this->error("开始时间不能大于结束时间！");
		$where .= " and a.addtime >= '".$start_time."' and a.addtime <= '".$end_time . " 23:59:59"."'";
		
		//数据分页
		$p = (int)$p <= 0 ? 1 : (int)$p;
		$pize = 15;
		$start = ($p - 1) * $pize;
		
		//统计总数
		$sql = "select count(a.id) as c from log_atm a,user b,agent c " . $where;
		$total = $this->db->query($sql)->row_array();
		$total = $total["c"];

		//分页查询
		$sql = "select a.*,b.username,b.realname,c.agent_name  from log_atm a ,user b ,agent c " . $where . " order by a.id DESC limit $start, $pize";
		$list = $this->db->query($sql)->result_array();
		
		//分页展示
		$url = "/" . $this->_controller . "/" . $this->_action . "/" . $aid. "/" . $ispay. "/" . $username . "/" . $start_time . "/" . $end_time . "/";
		$page = page_format($total, $pize, $p, $url);
		
		//查询代理
		$sql = "select id,agent_name,username from agent";
		$agent = $this->db->query($sql)->result_array();
		
		//分配展示
		$this->assign("list", $list);
		$this->assign("agent",$agent);
		$this->assign("page", $page);
		$this->assign("total", $total);
		$this->assign("aid", $aid);
		$this->assign("ispay", $ispay);
		$this->assign("username", $username);
		$this->assign("begin_time", $start_time);
		$this->assign("end_time", $end_time);
		$this->display();
	}
	
	/**
     * 提现审核
     */
	public function change($id = 0, $ispay = 0){
		if(IS_POST){
			//接收参数
			$id = intval($id);
			$ispay = intval($ispay);
			
			//设置参数
			if(0 >= $id) ajax_return(0, "非法请求");
			if($ispay != 2) $ispay = 1;
			
			//查询记录
			$sql = "select ispay,money,uid,aid from log_atm where id = $id and ispay = 0";
			$row = $this->db->query($sql)->row_array();
			if(! $row) ajax_return(0, "数据不存在");
			
			//开启事务
			$this->db->trans_begin();

			//驳回提现
			if($ispay == 2){
				$money = $row["money"];
				//更新账户
				$result = common_user($row["uid"], $money, $money * 10, 0, "驳回提现申请:" . $money . "元");
				if (! $result) {
					//事务回滚
					$this->db->trans_rollback();
					ajax_return(0, "入金失败");
				}
				$msg = "驳回提现成功";
			} else{
				$msg = "提现审核成功";
			}
			
			//更新状态
			$sql = "update log_atm set ispay = $ispay, uptime = CURRENT_TIMESTAMP where id = $id";
			$this->db->query($sql);
			
			//事务提交
			$this->db->trans_commit();
			
			//返回结果
			ajax_return(1, $msg);
		}
	}
}
